import { reqGetGoodsInfo, reqAddOrUpdateShopCart } from "@/api/detail";
import { getUUID } from "@/until/uuid_token";
const state = {
  goodsInfo: {},
  uuid_token: getUUID(), //游客的临时身份
};
const mutations = {
  //获取产品信息的数据
  GETGOODSINFO(state, goodsInfo) {
    state.goodsInfo = goodsInfo;
  },
};
const actions = {
  //获取产品信息的数据
  async getGoodsInfo({ commit }, params) {
    let res = await reqGetGoodsInfo(params);
    // console.log(res);
    if (res.code == 200) {
      commit("GETGOODSINFO", res.data);
    }
  },
  //加入｜更新购物
  async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
    let res = await reqAddOrUpdateShopCart(skuId, skuNum);
    console.log(res);
    if (res.code == 200) {
      return "ok";
    } else {
      new Promise.reject(new Error("fail"));
    }
  },
};
const getters = {
  //路径导航简化的数据
  categoryView(state) {
    return state.goodsInfo.categoryView || {};
  },
  //简化产品信息的数据
  skuInfo(state) {
    return state.goodsInfo.skuInfo || {};
  },
  //产品售卖属性的简化
  spuSaleAttrList() {
    return state.goodsInfo.spuSaleAttrList || [];
  },
};
export default {
  state,
  mutations,
  actions,
  getters,
};
